package com.taobao.askwho.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;
import org.mcavallo.opencloud.Cloud;
import org.mcavallo.opencloud.Tag;

import com.taobao.askwho.ao.TagAo;
import com.taobao.askwho.job.BuildLuceneIndex;

public class CloudTagUtils {
	//标签的显示数
	private  static String maxtagsToDispaly;
	//标签的最大权重
	private  static String maxWeigt;
	private  static String defultLinks;
	private String tagQuerySql;
	private static Logger logger = Logger.getLogger(CloudTagUtils.class);
	
	private final static CloudTagUtils cloudTagUtilPo = new CloudTagUtils();
	static {
		CloudTagUtils.setMaxtagsToDispaly("50");
		CloudTagUtils.setMaxWeigt("35");
		CloudTagUtils.setDefultLinks("/#");
		//cloudTagUtilPo.setDefultLinks("/askdba/main?searchText=%s");
	}
	/**
	 * getter and setter
	 * @return
	 */
	public String getTagQuerySql() {
		return tagQuerySql;
	}

	public String getDefultLinks() {
		return defultLinks;
	}

	private static void setDefultLinks(String defultLinks) {
		CloudTagUtils.defultLinks = defultLinks;
	}

	public String getMaxtagsToDispaly() {
		return maxtagsToDispaly;
	}

	public String getMaxWeigt() {
		return maxWeigt;
	}

	public static Cloud getCloud() {
		return cloud;
	}

	public static void setCloud(Cloud cloud) {
		CloudTagUtils.cloud = cloud;
	}

	public static void setMaxtagsToDispaly(String maxtagsToDispaly) {
		CloudTagUtils.maxtagsToDispaly = maxtagsToDispaly;
	}

	public static void setMaxWeigt(String maxWeigt) {
		CloudTagUtils.maxWeigt = maxWeigt;
	}

	public void setTagQuerySql(String tagQuerySql) {
		this.tagQuerySql = tagQuerySql;
	}

	/**
	 * constructor
	 */
	private CloudTagUtils(){
		super();
	}

	static Cloud cloud = new Cloud();
	private static List<Tag> tagList = new ArrayList<Tag>();

	public List<Tag> getCloudTags() {
		if(CloudTagUtils.tagList == null || CloudTagUtils.tagList.size() == 0) {
			//如果云没有,极有可能是第一次访问这个页面。
			calculateCloud();
			BuildLuceneIndex.buildLuceneIndexBegin();
		}
		return CloudTagUtils.tagList;
	}
	//风险点，可能以后会内存溢出 FIXME
	public synchronized static void calculateCloud() {
		cloud.setMaxTagsToDisplay(Integer.valueOf(maxtagsToDispaly));
		cloud.setMaxWeight(Double.valueOf(maxWeigt));
		cloud.setDefaultLink(defultLinks);
		cloud.clear();
		cloud.addText(getTagFromDB().toString());
		CloudTagUtils.tagList = cloud.tags();
	}

    /**
     * 从内存表中取数据
     */
    public synchronized static void calculateCloudFromMemory() {
        cloud.setMaxTagsToDisplay(Integer.valueOf(maxtagsToDispaly));
        cloud.setMaxWeight(Double.valueOf(maxWeigt));
        cloud.setDefaultLink(defultLinks);
        cloud.clear();
        cloud.addText(getTagFromMerory().toString());
        CloudTagUtils.tagList = cloud.tags();
    }

	private static StringBuilder getTagFromDB(){
		Map<String, Long> tagNameMap = TagAo.get().queryAllTaqsFromTagRelation();
		StringBuilder sb = new StringBuilder();
		for(Entry<String, Long> entry :tagNameMap.entrySet()) {
			String keyName = entry.getKey();
			Long count = entry.getValue();
			for(int i=0; i<count; i++) {
				sb.append(keyName + " ");
			}
		}
		logger.info("当前词条->" + tagNameMap.size());
		return sb;
	}

    /**
     * 从内存表中取数据
     * @return
     */
    private static StringBuilder getTagFromMerory(){
        Map<String, Long> tagNameMap = TagAo.get().queryAllTaqsFromMemory();
        StringBuilder sb = new StringBuilder();
        for(Entry<String, Long> entry :tagNameMap.entrySet()) {
            String keyName = entry.getKey();
            Long count = entry.getValue();
            for(int i=0; i<count; i++) {
                sb.append(keyName + " ");
            }
        }
        logger.info("当前词条->" + tagNameMap.size());
        return sb;
    }


	public static CloudTagUtils get() {
		return cloudTagUtilPo;
	}
	public static void main(String args[]){
		//		 CloudTagUtils cloudTagUtils=new CloudTagUtils();
		//		 cloudTagUtils.setMaxtagsToDispaly("25");
		//		 cloudTagUtils.setMaxWeigt("20");
		//		 cloudTagUtils.setDatabase(MysqlRouteBase.db_url);
		//		 cloudTagUtils.setUsername(MysqlRouteBase.db_name);
		//		 cloudTagUtils.setPassword(MysqlRouteBase.db_password);
		//		 cloudTagUtils.setDefultLinks("/#");
		//		 //cloudTagUtils.setDefultLinks("/askdba/main?searchText=%s");
		//		 cloudTagUtils.setTagQuerySql("select tagname from tag_relation where ispublic =1 ");
		//		 String text=cloudTagUtils.getTagFromDB().toString();
		//		 List<Tag> taglist=cloudTagUtils.getCloudTags(text);
		//		 for (Object tag : taglist) {
		//			 System.out.println(((Tag)tag).getWeightInt());
		//			 System.out.println(((Tag)tag).getName());
		//			 System.out.println(((Tag)tag).getLink());
		//		 }
	}


}
